package com.rop.znyx.activity.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rop.znyx.activity.service.ActivityInfoService;
import com.rop.znyx.common.result.Result;
import com.rop.znyx.model.activity.ActivityInfo;
import com.rop.znyx.model.product.SkuInfo;
import com.rop.znyx.vo.activity.ActivityRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 活动表 前端控制器
 * </p>
 *
 * @author rop
 * @since 2023-07-09
 */

@Api(tags = "营销活动管理")
@RestController
@RequestMapping("admin/activity/activityInfo")
@RequiredArgsConstructor

public class ActivityInfoController {

    private final ActivityInfoService activityInfoService;

    @ApiOperation("获取营销活动分页列表")
    @GetMapping("{page}/{limit}")
    public Result<IPage<ActivityInfo>> getList(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable("page") Long page,
                                               @ApiParam(name = "limit", value = "条数", required = true) @PathVariable("limit") Long limit) {
        Page<ActivityInfo> pageParam = new Page<>(page, limit);

        IPage<ActivityInfo> pageModel = activityInfoService.selectPage(pageParam);

        return Result.ok(pageModel);
    }

    /**
     * 根据id获取营销活动
     *
     * @param id 营销活动id
     * @return 营销活动详情
     */
    @ApiOperation("根据id获取营销活动")
    @GetMapping("get/{id}")
    public Result<ActivityInfo> getById(@PathVariable("id") Long id) {
        if (id != null) {
            ActivityInfo activityInfo = activityInfoService.getById(id);
            activityInfo.setActivityTypeString(activityInfo.getActivityType().getComment());
            return Result.ok(activityInfo);
        }
        return Result.fail();
    }

    /**
     * 新增营销活动
     */
    @ApiOperation("新增营销活动")
    @PostMapping("save")
    public Result<Boolean> save(@RequestBody ActivityInfo activityInfo) {
        activityInfo.setCreateTime(new Date());
        boolean success = activityInfoService.save(activityInfo);
        return success ? Result.ok() : Result.fail();
    }

    /**
     * 修改营销活动
     */
    @ApiOperation("修改营销活动")
    @PutMapping("update")
    public Result<Boolean> update(@RequestBody ActivityInfo activityInfo) {
        boolean success = activityInfoService.updateById(activityInfo);
        return success ? Result.ok() : Result.fail();
    }

    /**
     * 删除营销活动
     *
     * @param id 营销活动id
     * @return 删除结果
     */
    @ApiOperation("删除营销活动")
    @DeleteMapping("remove/{id}")
    public Result<Boolean> deleteById(@PathVariable("id") Long id) {
        boolean success = activityInfoService.removeById(id);
        return success ? Result.ok() : Result.fail();
    }

    /**
     * 批量删除营销活动
     *
     * @param ids 营销活动ids
     * @return 删除结果
     */
    @ApiOperation("批量删除营销活动")
    @DeleteMapping("batchRemove")
    public Result<Boolean> deleteByIds(@RequestBody List<Long> ids) {
        boolean success = activityInfoService.removeByIds(ids);
        return success ? Result.ok() : Result.fail();
    }

    /**
     * 营销活动规则相关接口
     *
     * @param id 营销活动id
     */
    @ApiOperation("营销活动规则相关接口")
    @GetMapping("findActivityRuleList/{id}")
    public Result<Map<String, Object>> findActivityRuleList(@PathVariable Long id) {
        Map<String, Object> map = activityInfoService.findActivityRuleList(id);
        return Result.ok(map);
    }

    /**
     * 新增活动规则
     *
     * @param activityRuleVo 活动规则实体
     * @return 新增结果
     */
    @ApiOperation(value = "新增活动规则")
    @PostMapping("saveActivityRule")
    public Result<Boolean> saveActivityRule(@RequestBody ActivityRuleVo activityRuleVo) {
        activityInfoService.saveActivityRule(activityRuleVo);
        return Result.ok();
    }

    /**
     * 根据关键字获取sku列表，活动使用
     */
    @GetMapping("findSkuInfoByKeyword/{keyword}")
    public Result<List<SkuInfo>> findSkuInfoByKeyword(@PathVariable("keyword") String keyword) {
        return Result.ok(activityInfoService.findSkuInfoByKeyword(keyword));
    }
}

